package com.example.impl;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzSchedulerExample {

    public static void  test() throws SchedulerException{
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // 创建JobDetail实例，并与MyScheduledJob类绑定
        JobDetail jobDetail = JobBuilder.newJob(MyScheduledJob.class)
                .withIdentity("myJob", "group1")
                .build();

        // 创建Trigger实例，并设置Cron表达式（例如：每5秒执行一次）
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger", "group1")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();

        // 将JobDetail和Trigger关联起来
        scheduler.scheduleJob(jobDetail, trigger);

        // 开始执行调度
        scheduler.start();
    }

    public static void main(String[] args) throws SchedulerException {
        test();
    }
}
